﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace NuMetaheuristics
{
    /// <summary>
    /// A class that contains a list of genomes (the population). This class 
    /// is the type which is operated on by algorithms.
    /// </summary>
    /// <typeparam name="G">The genotype class which inherits from IGenotype.</typeparam>
    [DataContract(Namespace = "http://numetaheuristics.codeplex.com/schemas")]
    public class Population<G>
        where G : IGenotype
    {
        /// <summary>
        /// The list of genomes (the population).
        /// </summary>
        [DataMember]
        public List<G> Genomes { get; set; }

        /// <summary>
        /// The default, no-argument constructor.
        /// </summary>
        public Population()
        {
            Genomes = new List<G>();
        }

        /// <summary>
        /// Contruct a population with a given size, using the given context to
        /// produce, initialize, and evaluate new genomes.
        /// </summary>
        /// <param name="size">The size of the population to be created</param>
        /// <param name="context">The <c>Context</c> which will be used to 
        /// produce, initialize, and evaluate new genomes.</param>
        public Population(int size, IContext<G> context)
        {
            Genomes = new List<G>(size);

            for (int i = 0; i < size; i++)
            {
                G g = context.Produce(true, true, true);
                Genomes.Add(g);
            }
            Genomes.Sort();
        }
    }
}
